package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Advice;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.util.StringUtil;

/**
 * 问卷建议
 * @author tngou@tngou.net
 *
 */
public class AdviceDao extends BaseDao {

	/**
	 * 取得访问的建议
	 * @param access
	 * @return
	 */
	public List<Advice> getAdvice(long access) {
		String sql ="SELECT advice.* FROM `iask_advice` advice ,`iask_accessresult` accessresult WHERE accessresult.access=? AND advice.question=accessresult.question AND advice.value=accessresult.value ";
		return QueryHelper.query(Advice.class, sql, access);
		
	}
	
	
	/**
	 * 
	 * @param page
	 * @param size
	 * @param main
	 * @param keyword
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,String keyword) {
		List<Advice> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT *";
		String sql_t="SELECT COUNT(id) ";
		String sql_o=" ORDER BY id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_advice`";
		if(main!=0)sql=sql+ " WHERE  main IN(0,?) ";
		else sql=sql+ " WHERE  main=? ";
		if(StringUtil.isNotEmpty(keyword))
		{
			sql=sql+" AND name LIKE ?";			
			Object[] params={main,"%"+keyword+"%"};
			list=QueryHelper.query_slice(Advice.class, sql_r+sql+sql_o, page, size, params);			
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}else{
			Object[] params={main};
			list=QueryHelper.query_slice(Advice.class, sql_r+sql+sql_o, page, size, params);	
			total=(int) QueryHelper.stat(sql_t+sql, params);
		}		
		return new PageUtil(list, page, size, total);
		
	}
}
